package net.zdreamer.shunyi.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocalWeatherForecast;
import com.amap.api.location.AMapLocalWeatherListener;
import com.amap.api.location.AMapLocalWeatherLive;
import com.amap.api.location.LocationManagerProxy;

import net.zdreamer.shunyi.R;
import net.zdreamer.shunyi.adapter.NewsListAdapter;
import net.zdreamer.shunyi.myview.XListView;
import net.zdreamer.shunyi.utils.ListUtils.NewsListInfos;

import java.util.ArrayList;


public class BanghuiNewsFragment extends Fragment implements AMapLocalWeatherListener,
        XListView.IXListViewListener, AdapterView.OnItemClickListener {


    private LocationManagerProxy mLocationManagerProxy;
    private TextView weath;
    private XListView mListView;


    private Handler mHandler;
    private int start = 0;
    private static int refreshCnt = 0;

    private NewsListInfos newsinfos;

    private NewsListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_banghui_news, container, false);

        mListView = (XListView) view.findViewById(R.id.xListView);
        mListView.addHeaderView(inithaed());
        mListView.setOnItemClickListener(this);
        mListView.setXListViewListener(this);
        mListView.setPullLoadEnable(true);
        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onStart() {
        super.onStart();

        mHandler = new Handler();


        newsinfos = new NewsListInfos();
        mAdapter = new NewsListAdapter(getActivity(), newsinfos.Newsinfo, R.layout.list_item);
        mListView.setAdapter(mAdapter);
        init();
    }

    private View inithaed() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view = inflater.inflate(R.layout.news_head_item, null);

        weath = (TextView) view.findViewById(R.id.banghuitianqi);
        return view;
    }


    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("刚刚");
    }

    /**
     * 注册天气监听
     */
    private void init() {
        mLocationManagerProxy = LocationManagerProxy.getInstance(getActivity());
        mLocationManagerProxy.requestWeatherUpdates(
                LocationManagerProxy.WEATHER_TYPE_LIVE, this);
    }


    @Override
    public void onWeatherLiveSearched(AMapLocalWeatherLive aMapLocalWeatherLive) {
        if (aMapLocalWeatherLive != null && aMapLocalWeatherLive.getAMapException().getErrorCode() == 0) {
            String city = aMapLocalWeatherLive.getCity();//城市
            String weather = aMapLocalWeatherLive.getWeather();//天气情况
            String temperature = aMapLocalWeatherLive.getTemperature();//温度
            String windDir = aMapLocalWeatherLive.getWindDir();//风向
            String windPower = aMapLocalWeatherLive.getWindPower();//风力
            String humidity = aMapLocalWeatherLive.getHumidity();//空气湿度
            String reportTime = aMapLocalWeatherLive.getReportTime();//数据发布时间
            String weatherinfo = city + "\n" + "天气： " + weather + "\n" + "温度： " + temperature + "℃" + "\n" + "风向： "
                    + windDir + "风" + "\n" + "风力： " + windPower + "级" + "\n" + "空气湿度：" + humidity + "%" ;
            weath.setText(weatherinfo);
        } else {
            // 获取天气预报失败
            Toast.makeText(getActivity(), "获取天气预报失败:" + aMapLocalWeatherLive.getAMapException().getErrorMessage(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onWeatherForecaseSearched(AMapLocalWeatherForecast aMapLocalWeatherForecast) {

    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                start = ++refreshCnt;
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
